<?php

class Form_Users extends Cms_FormProcessor
{
    protected $db = null;               // объект для соединения с базой данных
    public $news = null;                // объект класса DatabaseObject_User
    protected $_validateOnly = false;   // флаг, указывающий тип запроса: true = ajax
    
    public function __construct($db, $id = 0) {
        parent::__construct();
        
        $this->db = $db;
        $this->users = new Model_User($this->db);
        if ($id!='0'){
        $this->users->load($id);
        $this->id = $this->users->getId();
        $this->login = $this->users->login;
        $this->email = $this->users->email;
        $this->role = $this->users->role;
        $this->activated = $this->users->activated;
        $this->first_name = $this->users->first_name;
        $this->last_name = $this->users->last_name;
        $this->gender = $this->users->gender;
        $this->photo = $this->users->photo;
        $this->ban = $this->users->ban;
        
        }
        
    }
    public function validateOnly($flag) {
        $this->_validateOnly = (bool) $flag;
    }

    public function process(Zend_Controller_Request_Abstract $request) {
        
                // проверим логин
        $this->login = trim($request->getPost('login'));
        if (strlen($this->login) == 0)
            $this->addError('login', 'Вы должны указать логин');
        else if (!Model_User::IsValidUsername($this->login))
            $this->addError('login', 'Логин содержит недопустимые символы');
        else if ($this->users->usernameExists($this->login) && $this->users->login != $this->login)
            $this->addError('login', 'Выбранный вами логин уже занят');
        else
            $this->users->login = $this->login;

        // проверим, был ли введен новый пароль
        $this->password = $this->sanitize($request->getPost('password'));
        $this->password_again = $this->sanitize($request->getPost('password_again'));

        if (strlen($this->password) > 0 || strlen($this->password_again) > 0) {
            if (strlen($this->password) == 0)
                $this->addError('password', 'Пожалуйста, укажите новый пароль');
            else if (strlen($this->password_again) == 0)
                $this->addError('password_again', 'Пожалуйста, подтвердите свой пароль');
            else if ($this->password != $this->password_again)
                $this->addError('password_again', 'Введенные пароли не совпадают');
            else
                $this->users->password = $this->password;
        }
        
        // проверим название
        $this->email = trim($request->getPost('email'));

        if (strlen($this->email) == 0)
            $this->addError('email', 'Вы должны указать email');
        else
            $this->users->email = $this->email;
        
        $this->first_name = trim($request->getPost('first_name'));
        $this->users->first_name = $this->first_name;
        
        $this->last_name = trim($request->getPost('last_name'));
        $this->users->last_name = $this->last_name;
        
        
        $this->gender = trim($request->getPost('gender'));
        $this->users->gender = $this->gender;
        
        $this->role = trim($request->getPost('role'));
        $this->users->role = $this->role;
        
        $this->ban = trim($request->getPost('ban'));
        if ($this->ban == 'on')
        $this->users->ban = '1'; else  $this->users->ban = '0';
        
        $this->activated = trim($request->getPost('activated'));
           if ($this->activated == 'on')
        $this->users->activated = '1'; else  $this->users->activated = '0';
        
        $this->users->hash =  date("Y.m.d h:i:s");
        
        
        // если не было ошибок, сохраним пользователя
        if (!$this->_validateOnly && !$this->hasError()) {
            $this->users->save();
         
        }
        
        return !$this->hasError();
    }
}

  /*  public function init()
    {
        $this->setName('news');
        $id = new Zend_Form_Element_Hidden('id');
        $id->addFilter('Int');
        
        $isEmptyMessage = 'Значение является обязательным и не может быть пустым';
        
        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Название')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty', true,
                array('messages' => array('isEmpty' => $isEmptyMessage))
            );
        
        $h1 = new Zend_Form_Element_Text('h1');
        $h1->setLabel('Тег H1')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty', true,
                array('messages' => array('isEmpty' => $isEmptyMessage))
            );
        
        $description = new Zend_Form_Element_Text('description');
        $description->setLabel('Тег description')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty', true,
                array('messages' => array('isEmpty' => $isEmptyMessage))
            );
        
        $keywords = new Zend_Form_Element_Text('keywords');
        $keywords->setLabel('Тег keywords1')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty', true,
                array('messages' => array('isEmpty' => $isEmptyMessage))
            );
        
        $alias = new Zend_Form_Element_Text('alias');
        $alias->setLabel('SEO URL')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty', true,
                array('messages' => array('isEmpty' => $isEmptyMessage))
            );
        
        $short = new Zend_Form_Element_Textarea('short');
        $short->setLabel('Краткий текст')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty', true,
                array('messages' => array('isEmpty' => $isEmptyMessage))
            );
        
        $full = new Zend_Form_Element_Textarea('full');
        $full->setLabel('Полный текст')
            ->setRequired(true)
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty', true,
                array('messages' => array('isEmpty' => $isEmptyMessage))
            );
          
        
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setAttrib('id', 'submitbutton');
        $this->addElements(array($id,$name,$h1,$description,$keywords,$alias,$short,$full, $submit));
    }
}*/